<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>ActiveMqProducerApplication</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <!-- CSS -->
    <!--<link th:href="@{/Pic/supersized.css}" rel="stylesheet" type="text/css">-->
    <!--<link th:href="@{/Pic/style.css}" rel="stylesheet" type="text/css">-->
    <!--<link th:href="@{/favicon.ico}" rel="icon" type="image/x-icon">-->
    <!-- Javascript -->
    <script th:src="@{/jquery.min.js}"></script>
    <script th:inline="javascript">
        /*<![CDATA[*/
        //当清除缓存时会是这个样子[/;jsessionid=E6DD06F2268E7004E947863FE86FCA06]
        var path = (/*[[@{/}]]*/ +"").split(";")[0];
        /*]]>*/

        var timerId = null;

        $(function () {
            $("#startBut").attr('disabled', false);
            $("#stopBut").attr('disabled', true);
        });

        function startSend() {
            var taskNum = $("#taskNum").val();
            var maxNum = $("#maxNum").val();
            $.post(path + "startSend", {taskNum: taskNum, maxNum: maxNum}, function (result) {
                $("#startBut").attr('disabled', true);
                $("#stopBut").attr('disabled', false);
                timerId = setInterval(function () {
                    getMsgCount();
                }, 1000);
                alert(result.message);
            }, "json")
        }

        function stopSend() {
            $.post(path + "stopSend", {}, function (result) {
                $("#startBut").attr('disabled', false);
                $("#stopBut").attr('disabled', true);
                if (timerId != null) {
                    clearInterval(timerId);
                    timerId = null;
                }
                alert(result.message);
            }, "json")
        }

        function getMsgCount() {
            $.post(path + "getMsgCount", {}, function (result) {
                if (result.success) {
                    var d = result.data;
                    var text = "正在发送第: " + d.noIndex + " 条消息,发送成功: <span style='color: green'>" + d.successCount +
                        "</span> ,发送失败: <span style='color: red'>" + d.failCount + "</span> ,已耗时: " + d.timeCounnt;
                    $("#sendCount").html(text);
                    if (d.sendDown) {
                        $("#startBut").attr('disabled', false);
                        $("#stopBut").attr('disabled', true);
                        clearInterval(timerId);
                        timerId = null;
                        alert("发完完成！");
                    }
                }

            }, "json");
        }
    </script>
</head>
<body>

<div style="padding: 10px; background-color: darkgrey;">
    <h1>ActviceMq P2P Send</h1>
    任务数:<input type="text" value="1000" id="taskNum"> 发送数量:<input type="text" value="100000" id="maxNum">
    <button onclick="startSend()" id="startBut" type="button">开始生产消息</button>
    <button onclick="stopSend()" id="stopBut" type="button">停止生产消息</button>
    <br>
    <span id="sendCount"></span>
</div>

</body>
</html>